﻿using FlexiCache;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Web;
using System.Collections.Specialized;
using UnitTests.IISSimulator;

namespace FlexiCacheTests
{
    /// <summary>
    ///This is a test class for HttpResponseExtensionsTest and is intended
    ///to contain all HttpResponseExtensionsTest Unit Tests
    ///</summary>
    [TestClass()]
    public class HttpResponseExtensionsTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for RemoveOutputCacheItem
        ///</summary>
        [TestMethod()]
        public void RemoveOutputCacheItemTest()
        {
            string requestUrl = @"http://www.test.com/clo/clo/assignments/courses/courseentitypresenter.rest?courseid=259899&username=Lenny";
            string cacheKey = @"a2/clo/clo/assignments/courses/courseentitypresenter.restHQNcourseidV259899NusernameVLennyFCDE";
            
            ExternalOutputCacheProvider provider = new ExternalOutputCacheProvider();
            provider.RemoveAll();
            provider.Add("089776446", "Document # 5", DateTime.UtcNow.AddSeconds(100));
            provider.Add("089776447", "Document # 6", DateTime.UtcNow.AddSeconds(100));

            HttpSimulator sim = null;
            try
            {
                sim = new HttpSimulator("/", true);
                NameValueCollection vars = new NameValueCollection();
                sim.SimulateRequest(new Uri(requestUrl), vars);

                provider.Add(cacheKey, "Document # 7", DateTime.UtcNow.AddSeconds(100));

                HttpContext.Current.Response.RemoveOutputCacheItem("", new string[] { "courseid=259899" });
                long obj = provider.GetCount();

                Assert.AreEqual(2, obj, "The number of items should be 2.");
            }
            finally
            {
                if (sim != null)
                    sim.Dispose();
            }
        }
    }
}
